﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class PlayerData : MonoBehaviour
{
    public CardStore CardStore;
    public int playerCoins;
    public int[] playerCards;
    public int[] playerDeck;
    public CharacterCard CharacterCard;
    public int maxMana;

    public TextAsset playerData;
    // Start is called before the first frame update
    void Start()
    {
        //先加载卡牌在加载数据
        CardStore.LoadCardData();
        LoadPlayerData();
        InitializeManaFromCharacterCard();
    }

    // Update is called once per frame
    void Update()
    {

    }
    public PlayerData(int maxMana)
    {
        this.maxMana = maxMana;
    }

    public void RestoreMana()
    {
        if (CharacterCard != null)
        {
            CharacterCard.mana = maxMana; // 从角色卡恢复魔力值

        }
        else
        {
            Debug.LogWarning("角色卡未初始化，无法恢复魔力值！");
        }
    }
    // 初始化魔力值（从角色卡获取）
    private void InitializeManaFromCharacterCard()
    {
        if (CharacterCard != null)
        {
            maxMana = CharacterCard.mana;       // 最大魔力值等于角色卡的 mana
        }
        else
        {
            Debug.LogWarning("角色卡未初始化，魔力值无法设置！");
        }
    }
    public void LoadPlayerData()
    {
        playerCards = new int[CardStore.cardList.Count];
        playerDeck = new int[CardStore.cardList.Count];
        string[] dataRow = playerData.text.Split('\n');
        foreach (var row in dataRow)
        {
            string[] rowArray = row.Split(',');
            if (rowArray[0] == "#")
            {
                continue;
            }
            else if (rowArray[0] == "coins")
            {
                playerCoins = int.Parse(rowArray[1]);
            }
            else if (rowArray[0] == "card")
            {
                int id = int.Parse(rowArray[1]);
                int num = int.Parse(rowArray[2]);
                //载入玩家数据
                //确保id在有效范围内，避免数组越界或无效赋值
                playerCards[id] = num;
            }
            else if (rowArray[0] == "deck")
            {
                int id = int.Parse(rowArray[1]);
                int num = int.Parse(rowArray[2]);
                //载入玩家数据
                //确保id在有效范围内，避免数组越界或无效赋值
                playerDeck[id] = num;
            }
            else if (rowArray[0] == "character")
            {
                int characterId = int.Parse(rowArray[1]);
                // 根据 ID 加载角色卡
                if (characterId >= 0 && characterId < CardStore.characterCardList.Count)
                {
                    CharacterCard = CardStore.characterCardList[characterId];
                }
            }

        }
    }
    public void SavePlayerData()
    {
        //待完善
        //csv文件无法实时更新，已解决在编辑器实时更新，但最后构筑的时候是无法更新的
        //需之后修改为json文件进行读取
        string path = Application.dataPath + "/datas/playerdata.csv";

        List<string> datas = new List<string>();
        datas.Add("coins," + playerCoins.ToString());
        for (int i = 0; i < playerCards.Length; i++)
        {
            if (playerCards[i] != 0)
            {
                datas.Add("card," + i.ToString() + "," + playerCards[i].ToString());
            }
        }
        //保存卡组
        for (int i = 0; i < playerDeck.Length; i++)
        {
            if (playerDeck[i] != 0)
            {
                datas.Add("deck," + i.ToString() + "," + playerDeck[i].ToString());
            }
        }
        if (CharacterCard != null)
        {
            int characterId = CardStore.characterCardList.IndexOf(CharacterCard);
            if (characterId != -1)
            {
                datas.Add("character," + characterId.ToString());
            }
        }

        //保存数据
        File.WriteAllLines(path, datas);
#if UNITY_EDITOR
        AssetDatabase.Refresh();
#endif
    }
}
